package com.process.controller;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import com.process.Imp.servUtil.FileDetailService;
import com.process.controller.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @ClassName: FileDetailController
 * @Author: 殷凡卓
 * @Date: 2023/4/19 16:52
 */

@Slf4j
@RestController
@CrossOrigin(origins = "http://localhost:5173/")
public class FileDetailController {
    @Autowired
    private FileStorageService fileStorageService;//注入实列
    @Autowired
    private FileDetailService fileDetailService;

    /**
     * 上传图片，成功返回文件信息
     * 图片处理使用的是 https://github.com/coobird/thumbnailator
     */
    @PostMapping("/upload-image")
    public R uploadImage(MultipartFile file) {
        System.out.println(file);
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath("ImgDownLoad/") //保存到相对路径下，为了方便管理，不需要可以不写
                .image(img -> img.size(1000, 1000))  //将图片大小调整到 1000*1000
                .thumbnail(th -> th.size(200, 200))  //再生成一张 200*200 的缩略图
                .upload();
//        Integer record = fileDetailService.record(fileInfo) == true ? Code.SAVE_OK : Code.SAVE_ERR;

        return new R(1,fileInfo);
    }

    /**
     * 上传文件到指定存储平台，成功返回文件信息
     */
    @PostMapping("/upload")
    public R uploadPlatform(MultipartFile file , ServletRequest servletRequest) {

        log.info("上传文件controller");
        log.info(file.toString());
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String userName = (String)request.getAttribute("username");
        System.out.println("zzzzzz"+userName);
        System.out.println(file);

        FileInfo fileInfo = fileStorageService.of(file)
                .setPlatform("local-1")    //使用指定的存储平台
                .putAttr("pricticalId",333)
                .putAttr("submitter",userName)
                .putAttr("mutualEvalu",111)
                .putAttr("processId",222)
                .upload();
//        Integer record = fileDetailService.record(fileInfo) == true ? Code.SAVE_OK : Code.SAVE_ERR;


        return new R(1,fileInfo);
    }




}
